<style>
    .form-copy {
        margin-right: 5px;
        border-radius: 0;
        box-shadow: none;
        padding: 4px 12px 7px 12px;
        font-size: 14px;
        line-height: 1.42857143;
        color: #555;
        border: 1px solid #ccc;
        transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    }
    .select2 {
        margin-right: 5px
    }
</style>

<div class="btn-group" style="margin-right: 10px">
    <form class="form-inline" action="{{ $action }}" pjax-container method="get">
        <input style="width: 110px;" type="text" id="start_date" name="start_date" value="{{ $filtersBlade['start_date'] }}" class="form-copy" placeholder="选择开始日期">
        <input style="width: 110px;" type="text" id="end_date" name="end_date" value="{{ $filtersBlade['end_date'] }}" class="form-copy" placeholder="选择截止日期">
{{--        <select id="file_type" name="file_type">--}}
{{--            {{ $filtersBlade['file_type'] }}--}}
{{--        </select>--}}
        <div class="input-group">
            <button style="margin-left: 10px;" class="btn btn-sm btn-info submit"><i class="fa fa-search"></i>&nbsp;&nbsp;{{ trans('admin.search') }}</button>
            <a style="margin-left: 10px;" href="{{ $action }}" class="btn btn-sm btn-default"><i class="fa fa-undo"></i>&nbsp;&nbsp;{{ trans('admin.reset') }}</a>
            <button type="button" style="margin-left: 10px;" class="btn btn-sm btn-info create-file">生成文件</button>
        </div>
    </form>
</div>
<script language="JavaScript" type="text/javascript">
    $(function () {
        $('#start_date').datetimepicker({"format":"YYYY-MM-DD","locale":"zh-CN","allowInputToggle":true});
        $('#end_date').datetimepicker({"format":"YYYY-MM-DD","locale":"zh-CN","maxDate":moment(),"allowInputToggle":true});
        // $('#file_type').select2({placeholder: '值类型', allowClear: true, width: '120px'});
        var checkForm = function(){
            var startDate = $('#start_date').val();
            var endDate = $('#end_date').val();
            if(!startDate){
                swal('开始日期为空', '', 'error');
                return false;
            }
            if(!endDate){
                swal('截止日期为空', '', 'error');
                return false;
            }
            var startTime = Date.parse(startDate);
            var endTime = Date.parse(endDate);
            if(startTime > endTime){
                swal('开始日期不能大于截止日期', '', 'error');
                return false;
            }
            var time = endTime - startTime;
            var day = time / 1000 / 60 / 60 / 24;
            if(day > 180){
                swal('日期间隔过大，超过180天请分批下载', '', 'error');
                return false;
            }
            // var fileType = $('#file_type').val();
            // if(!fileType){
            //     swal('请选择文件类型', '', 'error');
            //     return false;
            // }
            return true;
        };
        $('.create-file').unbind('click').click(function(e) {
            e.preventDefault();
            if(checkForm()){
                var startDate = $('#start_date').val();
                var endDate = $('#end_date').val();
                // var fileType = $('#file_type').val();
                var fileType = 'sku';
                $.ajax({
                    method: 'post',
                    url: '/xqc/sales-report/sv-export-file',
                    data: {
                        _token: "{{ csrf_token() }}",
                        param: {start_date:startDate,end_date:endDate,file_type:fileType},
                    },
                    success: function (data) {
                        // $.pjax.reload('#pjax-container');

                        if (typeof data === 'object') {
                            if (data.status) {
                                swal(data.message, '', 'success');
                            } else {
                                swal(data.message, '', 'error');
                            }
                        }
                    }
                });
            }
        });
    });
</script>
